﻿namespace Sai.Test.Introspection
{
    using System.Linq;
    using Sai.Core;
    using Sai.Core.Introspection;
    using Sai.Test.TestDataBuilders;
    using Sai.Test.XUnit;
    using Xunit;

	public class MethodDeclarationFacts
	{
		public class TheToCodeMethod
		{
            [Fact, InMemoryTest]
            public void Returns_ReturnType_Name_Empty_Parentheses_If_No_Parameters()
            {
                string methodName = "Example";
                var returnType = new TypeName(new FullyQualifiedName("string"));

                var method = MethodDeclarationBuilder.Method()
                                .Named(methodName)
                                .WithReturnType(returnType)
                                .Build();

                SourceCode generated = method.ToCode();

                var content = generated.Content;

                Assert.Equal(5, content.Count);
                Assert.Equal("kw", content[0].Name);
                Assert.Equal("public", content[0].Value);
                Assert.Equal("tn", content[1].Name);
                Assert.Equal("string", content[1].Value);
                Assert.Equal("ident", content[2].Name);
                Assert.Equal("Example", content[2].Value);
                Assert.Equal("punc", content[3].Name);
                Assert.Equal("(", content[3].Value);
                Assert.Equal("punc", content[4].Name);
                Assert.Equal(")", content[4].Value);
            }

            [Fact, InMemoryTest]
            public void Returns_ReturnType_Name_Signature_If_Parameters()
            {
                string methodName = "Example";
                var returnType = new TypeName(new FullyQualifiedName("string"));

                var method = MethodDeclarationBuilder.Method()
                                .Named(methodName)
                                .WithReturnType(returnType)
                                .WithParameter("int")
                                .WithParameter("float")
                                .Build();

                SourceCode generated = method.ToCode();

                var content = generated.Content;

                Assert.Equal(8, content.Count);
                Assert.Equal("kw", content[0].Name);
                Assert.Equal("public", content[0].Value);
                Assert.Equal("tn", content[1].Name);
                Assert.Equal("string", content[1].Value);
                Assert.Equal("ident", content[2].Name);
                Assert.Equal("Example", content[2].Value);
                Assert.Equal("punc", content[3].Name);
                Assert.Equal("(", content[3].Value);
                Assert.Equal("tn", content[4].Name);
                Assert.Equal("int", content[4].Value);
                Assert.Equal("punc", content[5].Name);
                Assert.Equal(",", content[5].Value);
                Assert.Equal("tn", content[6].Name);
                Assert.Equal("float", content[6].Value);
                Assert.Equal("punc", content[7].Name);
                Assert.Equal(")", content[7].Value);
            }
		}
	}
}
